<template>
  <div class="categories">
    <el-button @click="showAdd" class="addbtn" type="primary"
      >添加分类</el-button
    >
    <el-table
    id="fff"
      :data="catelist"
      style="margin-bottom: 20px;line-height: 0px;"
      row-key="cat_id"
      border
      :tree-props="{ children: 'children', hasChildren: '0' }"
      show-header
      >
      <el-table-column type="index" label="#"> </el-table-column>
      <el-table-column prop="cat_name" label="分类名称" row-key="cat_id">
      </el-table-column>
      <el-table-column label="是否有效">
        <template slot-scope="scope">
          <i
            style="color:lightgreen"
            class="el-icon-success"
            v-if="scope.row.cat_deleted == false"
          ></i>
          <i style="color:red" class="el-icon-error" v-else></i>
        </template>
      </el-table-column>
      <el-table-column label="排序">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.cat_level == 0" size="mini">一级</el-tag>
          <el-tag
            v-else-if="scope.row.cat_level == 1"
            size="mini"
            type="success"
            >二级</el-tag
          >
          <el-tag v-else size="mini" type="warning">三级</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            @click="showeditCate(scope.row)"
            size="mini"
            type="primary"
            icon="el-icon-edit"
            >编辑</el-button
          >
          <el-button
            @click="showdeleteCate(scope.row)"
            size="mini"
            type="danger"
            icon="el-icon-delete"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[2, 4, 5, 10]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>

    <el-dialog title="编辑分类" :visible.sync="editdialogVisible">
      <el-form
        :model="editForm"
        :rules="addFormRules"
        ref="editruleFormRef"
        label-width="80px"
      >
        <el-form-item label="分类名称" prop="cateName">
          <el-input v-model="editForm.cat_name"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editCate">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 添加分类对话框 -->
    <el-dialog
      @close="addCateClosed"
      title="添加分类"
      :visible.sync="adddialogVisible"
    >
      <el-form
        :model="addCateForm"
        :rules="addFormRules"
        ref="addCateFormRef"
        label-width="80px"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类" prop="cat_name">
          <el-cascader
            v-model="selectedKeys"
            :options="parentCateList"
            :props="{
              expandTrigger: 'hover',
              value: 'cat_id',
              label: 'cat_name',
              children: 'children',
            }"
            @change="parentCateChange"
            clearable
            change-on-select
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="adddialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      catelist: [],
      editForm: {
        cat_name: "",
        cat_id: "",
      },
      editdialogVisible: false,
      addFormRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          {
            min: 1,
            max: 20,
            message: "分类名称长度在 1 到 20 个字符",
            trigger: "blur",
          },
        ],
      },
      adddialogVisible: false,
      addCateForm: {
        cat_pid: "",
        cat_level: "",
      },
      parentCateList: [],
      selectedKeys: [],
    };
  },
  mounted() {
    this.getCategories();
  },
  methods: {
    getCategories() {
      this.$http({
        url: "categories",
        method: "get",
        data: {
          type: this.queryInfo,
        },
      }).then((res) => {
        console.log(res);
        this.catelist = res.data;
        console.log(this.catelist);
      });
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getCategories();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getCategories();
    },
    showeditCate(cate) {
      this.editdialogVisible = true;
      this.$http({ url: `categories/${cate.cat_id}`, method: "get" }).then(
        (res) => {
          console.log(res);
          this.editForm.cat_name = res.data.cat_name;
          this.editForm.cat_id = res.data.cat_id;
        }
      );
    },
    editCate() {
      this.$http({
        url: `categories/${this.editForm.cat_id}`,
        method: "put",
        data: {
          cat_name: this.editForm.cat_name,
        },
      }).then((res) => {
        console.log(res);
        this.$message({
          type: "success",
          message: res.meta.msg,
        });
        this.editdialogVisible = false;
        this.getCategories();
      });
    },
    showdeleteCate(cate) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: `categories/${cate.cat_id}`,
            method: "delete",
          }).then((res) => {
            console.log(res);
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getCategories();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      this.getCategories();
    },
    showAdd() {
      this.adddialogVisible = true;
      this.$http({
        url: "categories",
        method: "get",
        data: {
          params: { type: 2 },
        },
      }).then((res) => {
        // console.log(res);
        this.parentCateList = res.data;
        console.log(this.parentCateList);
      });
    },
    addCateClosed() {
      this.$refs.addCateFormRef.resetFields();
      this.addCateForm.cat_level = 0;
      this.addCateForm.cat_id = 0;
      this.selectedKeys = [];
    },
    parentCateChange() {
      if (this.selectedKeys.length > 0) {
        this.addCateForm.cat_pid = this.selectedKeys[
          this.selectedKeys.length - 1
        ];
        this.addCateForm.cat_level = this.selectedKeys.length;
        return;
      } else {
        this.addCateForm.cat_pid = 0;
        this.addCateForm.cat_level = 0;
      }
    },
    //确定添加
    addCate() {
      this.$http({
        url: "categories",
        method: "post",
        data: {
          cat_pid: this.addCateForm.cat_pid,
          cat_name: this.addCateForm.cat_name,
          cat_level: this.addCateForm.cat_level,
        },
      }).then((res) => {
        console.log(res);
        this.adddialogVisible = false;
        this.getCategories();
        this.$message({
          type: "success",
          message: res.meta.msg,
        });
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.addbtn {
  margin: 10px;
}

.el-main {
    background-color: #e9eef3;
    color: #333;
    text-align: center;
    line-height: 0px !important;
}
.categories {
  height: 0px;
}
.fff{
  line-height: 0px;
}
.el-main {
    background-color: #e9eef3;
    color: #333;
    text-align: center;
    /* line-height: 610px; */
}
</style>
